package com.example.aliceapp;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class CrosshairCanvas extends View {

	private Bitmap b;
	private float posX = 500;
	private float posY = 500;
	private static float MAXX = 800;
	private static float MAXY = 800;
	private Paint paint;
	private float offsetX;
	private float offsetY;
	float x;
	float y;

	public CrosshairCanvas(Context context) {
		super(context);
		b = BitmapFactory.decodeResource(getResources(), R.drawable.point);
		paint = new Paint();
		paint.setColor(Color.RED);
		paint.setStrokeWidth(10);
		b = Bitmap.createScaledBitmap(b, 100, 100, true);
		offsetX = b.getWidth() / 2;
		offsetY = b.getHeight() / 2;
		this.x = offsetX;
		this.y = offsetY;
	}

	public void setPos(float x, float y) {
		posX = MAXX / 2 + x * MAXX / 20;
		posY = MAXY / 2 + y * MAXY / 20;

		if (x < 0) {
			this.x = -offsetX;

		} else {
			this.x = offsetX;
		}
		if (y < 0) {
			this.y = -offsetY;
		} else {
			this.y = offsetY;
		}
	}

	@Override
	protected void onDraw(Canvas c) {
		super.onDraw(c);
		c.drawLine(MAXY / 2, MAXX / 2, posY, posX, paint);
		c.drawBitmap(b, posY - offsetY, posX - offsetX, paint);
		c.save();
	}

}
